package com.fangrui.controller;

import com.fangrui.anno.Log;
import com.fangrui.pojo.Dept;
import com.fangrui.pojo.DeptLog;
import com.fangrui.pojo.Result;
import com.fangrui.service.DeptLogService;
import com.fangrui.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

import static java.time.LocalDateTime.*;

//
// Created by ✎﹏ℳ๓独赏☆独醉 on 2025/4/22 18:16.
//
@Slf4j
@RestController
public class DeptController {
    //声明日志记录对象----可以通过注解简化@Slf4j
//    private static Logger log = LoggerFactory.getLogger(DeptController.class);
    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptLogService deptLogService;
//    @RequestMapping(value="/depts",method = RequestMethod.GET)//可以简化
    @GetMapping("/depts")
    public Result getAllDept(){
        log.info("部门列表查询");
        List<Dept> deptList = deptService.getAllDept();
        if(deptList == null){
            return Result.error("未查询到数据");
        }else{
            return Result.success(deptList);
        }
    }
    @Log
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping("depts/{id}")
    public Result deleteDept(@PathVariable("id") Integer id) throws Exception {
        log.info("根据id删除部门：{}",id);
        int flagDept = deptService.deleteDept(id);
        int flagEmp = deptService.deleteEmpByDeptId(id);
        DeptLog deptLog = new DeptLog();
        deptLog.setCreateTime(now());
        deptLog.setDescription("执行解散部门操作，解散了" + id + "部门");
        deptLogService.createDeptLog(deptLog);
//        int i = 1/0;
//        if(true){
//            throw new Exception("出错了");
//        }
        if(flagDept > 0){
            return Result.success();
        }else{
            return Result.error("删除失败");
        }
    }
    @Log
    @PostMapping("depts")
    public Result addDept(@RequestBody Dept dept){
        log.info("新增部门");
        int res = deptService.addDept(dept);
        if(res > 0){
            return Result.success();
        }else{
            return Result.error("新增部门失败");
        }
    }
    @GetMapping("depts/{id}")
    public Result getDept(@PathVariable("id") Integer id){
        Dept dept = deptService.getDept(id);
        return Result.success(dept);
    }
    @Log
    @PutMapping("depts")
    public Result updateDept(@RequestBody Dept dept){
        if(dept == null || dept.getId() == null){
            return Result.error("未查询到该部门");
        }
        Dept deptTemp1 = deptService.getDept(dept.getId());
        if(deptTemp1 == null){
            return Result.error("未查询到该部门");
        }
        Dept dept2 =  deptService.getDept(dept.getName());
        if(dept2 != null){
            return Result.error("部门已存在");
        }
        int res = deptService.updateDept(dept);
        if(res > 0){
            return Result.success();
        }else{
            return Result.error("更新失败");
        }
    }
}
